package com.nf.health.app.activity.my;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;


import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.activity.home.DoctorDetailsActivity;
import com.nf.health.app.activity.my.adapter.RegisterRecordAdapter;
import com.nf.health.app.activity.my.bean.RegisterBean;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.customview.DragListView;
import com.nf.health.app.inquiry.activity.AssessActivity;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.ViewUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 个人中心  挂号记录
 * Created by Administrator on 2017/4/12.
 */
public class RegisterRecordActivity extends BaseActivity {


    RegisterRecordAdapter register;

    @Bind(R.id.record_listview)
    DragListView recordListview;

    List<RegisterBean> list;

    List<RegisterBean> listAll = new ArrayList<RegisterBean>();
    @Bind(R.id.rl_nothings_image)
    RelativeLayout rlNothingsImage;

    private int pageNo = 1, pageSize = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_register_record);
        ButterKnife.bind(this);
        ViewUtil.setText(this, R.id.titlebar_title_tv, "挂号记录");
        initView();
    }

    private void initView() {
        recordListview.setRefreshableAndLoadMoreable(true, false);
        recordListview.setOnRefreshAndLoadMoreListener(new DragListView.onRefreshAndLoadMoreListener() {

            @Override
            public void onRefresh() {
                pageNo = 1;
                initData("refresh");
            }

            @Override
            public void onLoadMore() {
                pageNo++;
                initData("more");
            }
        });

    }

    private void initData(String refresh) {
        provider.requestRegistrtionList(refresh);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData("refresh");
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        if ("refresh".equals(actionName)) {
            recordListview.completeRefresh();
            list = (List<RegisterBean>) object;

            if(list.size()<1){
                rlNothingsImage.setVisibility(View.VISIBLE);
                recordListview.setVisibility(View.GONE);
            }else{
                rlNothingsImage.setVisibility(View.GONE);
                recordListview.setVisibility(View.VISIBLE);
            }
            listAll.clear();
            listAll.addAll(list);
            register = new RegisterRecordAdapter(this, listAll);
            recordListview.setAdapter(register);

            if (list.size() < pageSize) {
                recordListview.setRefreshableAndLoadMoreable(true, false);
            } else {
                recordListview.setRefreshableAndLoadMoreable(true, true);
            }

        } else if ("more".equals(actionName)) {
            recordListview.completeLoadMore();
            list.clear();
            list = (List<RegisterBean>) object;

            if (list.size() > 0) {
                listAll.addAll(list);
                register.notifyDataSetChanged();
            } else {
                showToast("没有更多数据~");
            }
            if (list.size() < pageSize) {
                recordListview.setRefreshableAndLoadMoreable(true, false);
            } else {
                recordListview.setRefreshableAndLoadMoreable(true, true);
            }
        } else if ("cancelregisterrecord".equals(actionName)) {
            showToast("取消成功");
            initData("refresh");
        }
        register.setOnMyClickListener(new OnMyClickListener() {
            @Override
            public void onClick(int position, String flag) {
                if ("评价".equals(flag)) {
                    Bundle commentBundle = new Bundle();
                    commentBundle.putString(AssessActivity.INQUIRY_ID, list.get(position).getId());
                    commentBundle.putString(AssessActivity.DOCTOR_ID, list.get(position).getDoctorid());
                    ActivityUtils.showActivity(RegisterRecordActivity.this, AssessActivity.class, commentBundle);
                } else if ("再次挂号".equals(flag)) {
                    Intent intent = new Intent(RegisterRecordActivity.this, DoctorDetailsActivity.class);
                    intent.putExtra(Constants.FROM, "registered");
                    intent.putExtra(Constants.DOCTOR_ID, list.get(position).getDoctorid());
                    startActivity(intent);
                } else if ("取消预约".equals(flag)) {
                    provider.requestCancelRegistrtionList(list.get(position).getId(), "cancelregisterrecord");
                }
            }
        });
    }
}
